package com.qdxwx.data.service.user;

import com.qdxwx.common.page.PageRequest;
import com.qdxwx.common.utils.TKString;
import com.qdxwx.data.mapper.user.UserVitaeMapper;
import com.qdxwx.models.user.UserVitae;
import com.qdxwx.models.user.UserVitaeQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class UserVitaeServiceImpl implements UserVitaeService {

    @Autowired
    private UserVitaeMapper userVitaeMapper;

    @Override
    public int insert(UserVitae userVitae) {
        userVitae.setCreateTime(LocalDateTime.now());
        return userVitaeMapper.insertUserVitae(userVitae);
    }

    @Override
    public UserVitae get(int id) {
        if (id < 1) return null;
        return userVitaeMapper.selectById(id);
    }

    @Override
    public UserVitae getByUserId(long userId) {
        if (userId < 1) return null;
        return userVitaeMapper.selectByUserId(userId);
    }

    @Override
    public List<UserVitae> getList(UserVitaeQuery query, PageRequest page) {
        return userVitaeMapper.selectList(query, page);
    }

    @Override
    public int count(UserVitaeQuery query) {
        return userVitaeMapper.selectCount(query);
    }

    @Override
    public int updata(UserVitae userVitae) {
        userVitae.setUpdateTime(LocalDateTime.now());
        return userVitaeMapper.updataUserVitae(userVitae);
    }

    @Override
    public int updateVitaePath(UserVitae userVitae) {
        if (userVitae.getId() < 1 || TKString.isEmail(userVitae.getVitaePath())) return 0;
        return userVitaeMapper.updateVitaePath(userVitae.getId(), userVitae.getVitaePath());
    }

    @Override
    public int delete(int id) {
        if (id < 1) return 0;
        return userVitaeMapper.deleteById(id);
    }
}
